package com.netease.service.impl;

import com.github.pagehelper.PageHelper;
import com.netease.mapper.WordSegmentLogMapperCustom;
import com.netease.service.WordSegmentLogService;
import com.netease.entity.Word;
import com.netease.mapper.WordSegmentLogMapper;
import com.netease.pojo.WordSegmentLog;
import com.netease.utils.DateUtil;
import com.netease.utils.PagedGridResult;
import com.netease.vo.WordSegmentLogVO;
import com.netease.vo.WordSegmentReferenceVO;
import org.n3r.idworker.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @projectName: modeling-dev
 * @package: com.netease.impl
 * @className: WordSegmentLogService
 * @author: aochong
 * @description: 分词日志服务接口
 * @date: 2023/4/3 19:49
 * @version: 1.0
 */
@Service
public class WordSegmentLogServiceImpl implements WordSegmentLogService {

    private static final Logger logger = LoggerFactory.getLogger(WordSegmentLogServiceImpl.class);

    @Autowired
    private WordSegmentLogMapper wordSegmentLogMapper;
    @Autowired
    private WordSegmentLogMapperCustom wordSegmentLogMapperCustom;

    @Autowired
    private Sid sid;

    @Override
    public void createWordSegmentLog(Word word) {
        WordSegmentLog wordSegmentLog = new WordSegmentLog();
        wordSegmentLog.setId(sid.nextShort());
        wordSegmentLog.setFieldCmmnt(word.getInitialName());
        wordSegmentLog.setRootName(word.getName());
        wordSegmentLog.setTranslt(word.getEngName());
        wordSegmentLog.setEngName(word.getEngAbbreviation());
        wordSegmentLog.setStartOffst(word.getStartOffset().toString());
        wordSegmentLog.setEndOffst(word.getEndOffset().toString());
        wordSegmentLog.setCreatedTime(DateUtil.dateToString(new Date()));
        wordSegmentLog.setUpdatedTime(DateUtil.dateToString(new Date()));

        wordSegmentLogMapper.insert(wordSegmentLog);
    }

    @Override
    public void updateWordSegmentLog(WordSegmentLog wordSegmentLog) {
        wordSegmentLogMapper.updateByPrimaryKey(wordSegmentLog);
    }

    @Override
    public List<WordSegmentLogVO> queryWordSegmentLog(WordSegmentLog wordSegmentLog) {
        return wordSegmentLogMapperCustom.getWordSegmentLogList(wordSegmentLog.getFieldCmmnt());
    }

    @Override
    public boolean queryWordSegmentLogIsExists(WordSegmentLog wordSegmentLog) {
        Example example = new Example(WordSegmentLog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("fieldCmmnt",wordSegmentLog.getFieldCmmnt());

        List<WordSegmentLog> wordSegmentLogList = wordSegmentLogMapper.selectByExample(example);

        return wordSegmentLogList.size() > 0;
    }

    @Override
    public PagedGridResult queryPagedWordSegmentLog(Integer page,Integer pageSize) {
        /**
         * page: 第几页
         * pageSize: 每页显示条数
         */
        PageHelper.startPage(page, pageSize);
        List<WordSegmentLog> wordSegmentLogs = wordSegmentLogMapper.selectAll();
        PagedGridResult pagedGridResult = RootInfoServiceImpl.setterPagedGrid(wordSegmentLogs, page);

        return pagedGridResult;
    }

    @Override
    public boolean deleteWordSegmentLog(int id) {
        System.out.println(wordSegmentLogMapper.deleteByPrimaryKey(id) );
        return true;
    }

    @Override
    public List<WordSegmentReferenceVO> queryWordSgmntRfrnc(String date) {
        return wordSegmentLogMapperCustom.getWordSegmentReferenceList(date.substring(0,10),date.substring(11,13));
    }
}
